<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
if(!in_array($f,array('index','edit','save','check','nocheck','delete','ajax_check','ajax_edit'))){
	exit('Access Denied!');
}
if(isset($_G['usergroup']['access']["shop$f"]) && !ican("shop$f")){
	msg('没有权限');
}
$m = new ShopModule;
$m->$f();
class ShopModule
{
	function index() {
		global $_G;
		$mod = getGP('mod','G') == 'uncheck' ? 'uncheck' : 'list';
		$wheresql = '';
		$page = max(1, getGP('page','G','int'));
		$pagesize = 10;
		$offset = ($page - 1) * $pagesize;
		$url = 'admin.php?m=shop&mod='.$mod;
		
		if (getGP('check','G') == 'yes') {
			$wheresql .= " AND a.status = 1";
			$url .= '&check=yes';
		} elseif (getGP('check','G') == 'no') {
			$wheresql .= " AND a.status = 0";
			$url .= '&check=no';
		}
		if ( $keyword = getGP('keyword','G') ) {
			$wheresql .= " AND (a.content LIKE '%$keyword%' OR a.username LIKE '%$keyword%' OR a.email LIKE '%$keyword%')";
			$url .= '&keyword='.rawurlencode($keyword);
		}
		if ($aid = getGP('aid','G','int')) {
			$wheresql .= " AND a.referid = '$aid'";
			$url .= '&aid='.$aid;
		}
		
		if($mod == 'uncheck'){
			$wheresql .= " AND s.status = 0";
		}
		$num = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."shops s WHERE 1 $wheresql");
	
		$result = $_G['db']->fetch_all("SELECT s.*,u.qq,u.phone,u.tel as shoptel,u.shopid as hasid FROM ".DB_PREFIX."shops s LEFT JOIN ".DB_PREFIX."users u ON s.uid = u.uid WHERE 1 $wheresql ORDER BY s.shopid DESC LIMIT $offset, $pagesize");
	
		include admintemplate('shop');
	}
	function edit(){
		global $_G;
		$id = getGP('id','G','int');
		$result = $_G['db']->fetch_one_array("SELECT s.*,u.qq,u.phone,u.tel as shoptel,u.shopid as hasid FROM ".DB_PREFIX."shops s LEFT JOIN ".DB_PREFIX."users u ON s.uid = u.uid WHERE s.shopid='$id'");
		$cates = $_G['cache']->get('category');
		$worktime1 = array('05:00','05:30','06:00','06:30','07:00','07:30','08:00','08:30','09:00','09:30','10:00','10:30','11:00','11:30');
		$worktime2 = array('12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30');
		m('addr');
		$region=AddrModule::getaddr($result['rid']);	
		include admintemplate('shop');
	}
	function save(){
		if(!IS_POST) {
			msg('禁止操作');
		}
		if(!ican("shopedit")){
			msg('没有权限');
		}
		global $_G;
		$result = array(
			'cid' => getGP('stype','P','int'),
			'shopname' => getGP('shopname','P'),
			'opentime' => getGP('opentime','P'),
			'closetime' => getGP('closetime','P'),
			'tel' => getGP('tel','P'),
			'minprice' => getGP('minprice','P'),
			'addr1' => getGP('addr1','P'),
			'addr2' => getGP('street1','P'),
			'bus' => getGP('bus','P'),
			'description' => getGP('description','P'),
			'tedian' => getGP('tedian','P'),
			'chengnuo' => getGP('chengnuo','P'),
			'sat' => getGP('res1','P','int'),
			'sun' => getGP('res2','P','int'),
			'rid' => getGP('rid','P','int'),
		) ;
		$_G['db']->update('shops',$result,array('shopid'=>getGP('shopid','P','int')));
		msg('修改成功','admin.php?m=shop&f=edit&id='.getGP('shopid','P','int'));
	}
	function check(){
		global $_G;
		$idarr = getGP('id','P','array');
		$aidarr = array();
		foreach ($idarr as $id) {
			if ($aid = $_G['db']->result("SELECT referid FROM ".DB_PREFIX."comment WHERE cid = '$id'")) {
				$_G['db']->query("UPDATE ".DB_PREFIX."article SET comments = comments + 1 WHERE aid = '$aid'");
			}
			$_G['db']->query("UPDATE ".DB_PREFIX."comment SET status = 1 WHERE cid = '$id'");
		}
		recache('lastest_comment');
		msg('操作成功','admin.php?m=comment');
	}
	function nocheck(){
		global $_G;
		$idarr = getGP('id','P','array');
		$aidarr = array();
		foreach ($idarr as $id) {
			if ($aid = $_G['db']->result("SELECT referid FROM ".DB_PREFIX."comment WHERE cid = '$id'")) {
				$_G['db']->query("UPDATE ".DB_PREFIX."article SET comments = comments - 1 WHERE aid = '$aid'");
			}
			$_G['db']->query("UPDATE ".DB_PREFIX."comment SET status = 0 WHERE cid = '$id'");
		}
		recache('lastest_comment');
		msg('操作成功','admin.php?m=comment');
	}
	function delete(){
		global $_G;
		if ($id = getGP('id','G','int')) {
			$_G['db']->query("DELETE FROM".table('comment')."WHERE shopid = '$id' ");
			$_G['db']->query("DELETE FROM".table('shops')."WHERE shopid = '$id' LIMIT 1");
			$_G['db']->query("DELETE FROM".table('product_consult')."WHERE shopid = '$id'");
			$_G['db']->query("DELETE FROM".table('product')."WHERE shopid = '$id'");
			$_G['db']->update('users',array('shopid'=>0),array('shopid'=>$id));
			
			$output['type'] = 'success';
			exit(json_encode($output));
		} else {
			$idarr = getGP('id','P','array');
			foreach ($idarr as $id) {
				$_G['db']->query("DELETE FROM".table('comment')."WHERE shopid = '$id' ");
				$_G['db']->query("DELETE FROM".table('shops')."WHERE shopid = '$id' LIMIT 1");
				$_G['db']->query("DELETE FROM".table('product_consult')."WHERE shopid = '$id'");
				$_G['db']->query("DELETE FROM".table('product')."WHERE shopid = '$id'");
				$_G['db']->update('users',array('shopid'=>0),array('shopid'=>$id));
			}
			recache('lastest_comment');
			msg('操作成功', 'admin.php?m=shop');
		}
	}
	function ajax_check(){
		global $_G;
		$id = getGP('id','G','int');
		$output = array();
		if ($id) {
			if ($comment = $_G['db']->fetch_one_array("SELECT status,uid FROM ".DB_PREFIX."shops WHERE shopid = '$id'")) {
				$output['type'] = 'success';
				if ($comment['status']) {
					$_G['db']->query("UPDATE ".DB_PREFIX."shops SET status = 0 WHERE shopid = '$id'");
					$output['data'] = 'nocheck';
				} else {
					$_G['db']->query("UPDATE ".DB_PREFIX."shops SET status = 1 WHERE shopid = '$id'");
					$output['data'] = 'check';
				}
			} else {
				$output['type'] = 'error';
				$output['data'] = '店铺不存在。';
			}
		} else {
			$output['type'] = 'error';
			$output['data'] = '缺少ID参数。';
		}
		exit(json_encode($output));
	}
	function ajax_edit(){
		global $_G;
		$id = getGP('id','P','int');
		$content = nl2br(getGP('content','P'));
		$output = array();
		if ( $id ) {
			$_G['db']->query("UPDATE ".DB_PREFIX."comment SET content = '$content' WHERE cid = '$id'");
			$output['type'] = 'success';
		} else {
			$output['type'] = 'error';
			$output['data'] = '更新失败';
		}
		exit(json_encode($output));	
	}
}
?>